import axios from "@/utils/requestdp";
import uuidv1 from "uuid/v1";

import dayjs from "dayjs";
import { option } from "runjs";

let zz = {
  watch: {},
  methods: {
    beijing() {
      this.beijing11();
    },
    beijing11() {
      require("@/views/tongyong/ThreeDimensional/components/PolylineTrail");
      let _this = this;
      var viewer = this._earth.czm.viewer;

      // console.log(dataa);C:\Users\Administrator\Desktop\FOUR\daping_front_8083\public\configuration\beijing\beijing.json

      var promise = Cesium.GeoJsonDataSource.load(
        "/configuration/beijing/beijing.json"
      );
      promise.then(function(dataSource) {
        viewer.dataSources.add(dataSource);
        var entities = dataSource.entities.values;

        for (var o = 0; o < entities.length; o++) {
          var entity = entities[o];

          //删除默认提示框
          entity.billboard = undefined;

          //判断点
          if (
            entity.id.split(".")[0] == "heatplant" ||
            entity.id.split(".")[0] == "boiler" ||
            entity.id.split(".")[0] == "measuredpoint"
          ) {
            //特殊点
            //热源厂 需要区分隔压站 锅炉房 热源
            // switch (entity.properties._type._value) {
            //   case "隔压站":
            //   case "燃气锅炉房":
            //   case "热电厂":

            let imgpath = "";

            switch (entity.id.split(".")[0]) {
              case "heatplant":
                imgpath = "redianchang-01";
                break;
              case "boiler":
                imgpath = "guolufang-01";
                break;
              case "measuredpoint":
                imgpath = "geyazhan-01";
                break;
              default:
                imgpath = "newFactory";
                break;
            }

            let nameid =
              entity.id +
              "|" +
              entity.properties.pid._value +
              "|" +
              entity.position._value;

            let ln = Cesium.Cartographic.fromCartesian(entity.position._value);

            let uniqueID = uuidv1();
            _this._earth.sceneTree.root.children.push({
              ref: uniqueID,
              czmObject: {
                customProp: "ghjghj",
                xbsjType: "Pin",
                xbsjGuid: "96f2033d-55cf-4677-b8c7-bde52f14b054",
                name: nameid,
                position: [ln.longitude, ln.latitude, ln.height],
                scale: 0.6,
                // disableDepthTestDistance: 0,
                imageUrl: `/configuration/logo/${imgpath}.png`,
                // _this.pointericon[entity.properties._type._value],
                pinBuilder: {},
                far: 1073741824
              }
            });

            // _this.pinarr.push(uniqueID);
            // }
            // }
            //     break;
            //   default:
            //     break;
            // }
          } else if (
            entity.id.split(".")[0] == "substation" ||
            entity.id.split(".")[0] == "tee"
          ) {
            //普通点------------------------------------------------------------------------------
            //热力站
            if (
              // entity.id.split(".")[0] == "tee" ||
              entity.id.split(".")[0] == "substation"
            ) {
              //判断视角高度
              //判断是否已经显示
              //普通点
              //------------------------------------++++++++++++++++++++++++++++++
              if (entity.properties.name._value != "") {
                if (
                  entity.properties.name._value.indexOf("T") == -1 &&
                  entity.properties.name._value.indexOf("V") == -1
                ) {
                  entity.point = new Cesium.PointGraphics({
                    color: Cesium.Color.fromCssColorString("#5D96B799"),
                    pixelSize: 20,
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                      10.0,
                      52820.0
                    ),
                    scaleByDistance: new Cesium.NearFarScalar(
                      500,
                      2,
                      52820,
                      0.01
                    )
                    // translucencyByDistance: 100,
                    // disableDepthTestDistance: 100.0
                  });
                }
              }
            }
          } else if (entity.id.split(".")[0] == "pipeline") {
            //-------------------------------------------------------------------------------------------------------------------------------
            //添加 ODline
            // entity.polyline = undefined;

            entity.polyline = {
              positions: entity.polyline.positions._value,
              width: 3,
              material: new Cesium.PolylineTrailLinkMaterialProperty(
                Cesium.Color.fromCssColorString("#b399ff"),
                5000
              ),
              // clampToGround: true,
              distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                0,
                500000
              )
              // scaleByDistance: new Cesium.NearFarScalar(500, 5, 52820, 0.001),
            };
          }
        }
      });

      //添加点击事件
      viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
        movement
      ) {
        var pickedFeature = viewer.scene.pick(movement.position);

        if (typeof pickedFeature != "undefined") {
          if (pickedFeature.id.properties == undefined) {
            _this.pointerclickbeijing({
              id: pickedFeature.id.name.split("|")[0],
              pid: pickedFeature.id.name.split("|")[1],
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });
          } else {
            _this.pointerclickbeijing({
              id: pickedFeature.id.id,
              pid: pickedFeature.id.properties.pid._value,
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });
          }
        }
      },
      Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },

    pointerclickbeijing(options) {
      //获取接口数据
      //数据返回显示弹窗
      //判断id
      let pid = options.pid;

      console.log(options.id);

      // return

      let reqshishi = "";
      let reqlishi = "";

      let type = options.id.split(".")[0];

      switch (options.id.split(".")[0]) {
        case "substation":
          //热力站
          reqshishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
                schema: "monitor",
                table: "substation"
              }
            ],
            search: "pid=" + pid
          };
          reqlishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
                schema: "monitor"
              }
            ],
            table: "substation",
            type: "history",
            search: "pid=" + pid,
            startTime: new Date(
              new Date().getTime() - 1 * 24 * 60 * 60 * 1000
            ).getTime(),
            endTime: new Date().getTime(),
            downsample: "2hour-last"
          };
          break;
        case "measuredpoint":
          //计量站
          reqshishi = {
            queries: [
              {
                metric: "pid,name,m_006t,m_007t,m_006p,m_007p,m_009q,m_009qc",
                schema: "monitor",
                table: "measuredpoint"
              }
            ],
            search: "pid=" + pid
          };
          reqlishi = {
            queries: [
              {
                metric: "pid,name,m_006t,m_007t,m_006p,m_007p",
                schema: "monitor"
              }
            ],
            table: "measuredpoint",
            type: "history",
            search: "pid=" + pid,
            startTime: new Date(
              new Date().getTime() - 1 * 24 * 60 * 60 * 1000
            ).getTime(),
            endTime: new Date().getTime(),
            downsample: "2hour-last"
          };
          break;
        case "boiler":
          //锅炉房
          reqshishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p,m_003q,m_003qc",
                schema: "monitor",
                table: "boiler"
              }
            ],
            search: "pid=" + pid
          };
          reqlishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
                schema: "monitor"
              }
            ],
            table: "boiler",
            type: "history",
            search: "pid=" + pid,
            startTime: new Date(
              new Date().getTime() - 1 * 24 * 60 * 60 * 1000
            ).getTime(),
            endTime: new Date().getTime(),
            downsample: "2hour-last"
          };
          break;
        case "heatplant":
          //锅炉房
          reqshishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p,m_003q,m_003qc",
                schema: "monitor",
                table: "heatplant"
              }
            ],
            search: "pid=" + pid
          };
          reqlishi = {
            queries: [
              {
                metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
                schema: "monitor"
              }
            ],
            table: "heatplant",
            type: "history",
            search: "pid=" + pid,
            startTime: new Date(
              new Date().getTime() - 1 * 24 * 60 * 60 * 1000
            ).getTime(),
            endTime: new Date().getTime(),
            downsample: "2hour-last"
          };
          break;

        default:
          break;
      }

      let req1 = axios.post("/open/queryDb", reqshishi);
      let req2 = axios.post("/open/queryDb", reqlishi);

      Promise.all([req1, req2]).then(res => {
        let html = "";
        let uniqueID = uuidv1();
        options.uid = uniqueID;
        options.diahtml = html;
        options.res = res;

        switch (type) {
          case "substation":
            options.kv = {
              gongwen: res[0].data[0]["m_001t"]
                ? res[0].data[0]["m_001t"]
                : "-",
              huiwen: res[0].data[0]["m_002t"] ? res[0].data[0]["m_002t"] : "-",
              gongya: res[0].data[0]["m_001p"] ? res[0].data[0]["m_001p"] : "-",
              huiya: res[0].data[0]["m_002p"] ? res[0].data[0]["m_002p"] : "-",
              liuliang: res[0].data[0]["m_001t"]
                ? res[0].data[0]["m_001t"]
                : "-",
              reliang: res[0].data[0]["m_001t"] ? res[0].data[0]["m_001t"] : "-"
            };

            options.chartkv = res[1].data.map(item => {
              return {
                gongwen: res[0].data[0]["m_001t"]
                  ? res[0].data[0]["m_001t"]
                  : "-",
                huiwen: res[0].data[0]["m_002t"]
                  ? res[0].data[0]["m_002t"]
                  : "-",
                gongya: res[0].data[0]["m_001p"]
                  ? res[0].data[0]["m_001p"]
                  : "-",
                huiya: res[0].data[0]["m_002p"] ? res[0].data[0]["m_002p"] : "-"
              };
            });
            break;
          case "measuredpoint":
            options.kv = {
              gongwen: res[0].data[0]["m_006t"]
                ? res[0].data[0]["m_006t"]
                : "-",
              huiwen: res[0].data[0]["m_007t"] ? res[0].data[0]["m_007t"] : "-",
              gongya: res[0].data[0]["m_006p"] ? res[0].data[0]["m_006p"] : "-",
              huiya: res[0].data[0]["m_007p"] ? res[0].data[0]["m_007p"] : "-",
              liuliang: res[0].data[0]["m_009q"]
                ? res[0].data[0]["m_009q"]
                : "-",
              reliang: res[0].data[0]["m_009qc"]
                ? res[0].data[0]["m_009qc"]
                : "-"
            };

            options.chartkv = res[1].data.map(item => {
              return {
                gongwen: res[0].data[0]["m_006t"]
                  ? res[0].data[0]["m_006t"]
                  : "-",
                huiwen: res[0].data[0]["m_007t"]
                  ? res[0].data[0]["m_007t"]
                  : "-",
                gongya: res[0].data[0]["m_006p"]
                  ? res[0].data[0]["m_006p"]
                  : "-",
                huiya: res[0].data[0]["m_007p"]
                  ? res[0].data[0]["m_007p"]
                  : "-",
                liuliang: res[0].data[0]["m_009q"]
                  ? res[0].data[0]["m_009q"]
                  : "-",
                reliang: res[0].data[0]["m_009qc"]
                  ? res[0].data[0]["m_009qc"]
                  : "-"
              };
            });
            break;
          case "heatplant":
          case "boiler":
            options.kv = {
              gongwen: res[0].data[0]["m_001t"]
                ? res[0].data[0]["m_001t"]
                : "-",
              huiwen: res[0].data[0]["m_002t"] ? res[0].data[0]["m_002t"] : "-",
              gongya: res[0].data[0]["m_001p"] ? res[0].data[0]["m_001p"] : "-",
              huiya: res[0].data[0]["m_002p"] ? res[0].data[0]["m_002p"] : "-",
              liuliang: res[0].data[0]["m_003q"]
                ? res[0].data[0]["m_003q"]
                : "-",
              reliang: res[0].data[0]["m_003qc"]
                ? res[0].data[0]["m_003qc"]
                : "-"
            };

            options.chartkv = res[1].data.map(item => {
              return {
                gongwen: res[0].data[0]["m_001t"]
                  ? res[0].data[0]["m_001t"]
                  : "-",
                huiwen: res[0].data[0]["m_002t"]
                  ? res[0].data[0]["m_002t"]
                  : "-",
                gongya: res[0].data[0]["m_001p"]
                  ? res[0].data[0]["m_001p"]
                  : "-",
                huiya: res[0].data[0]["m_002p"]
                  ? res[0].data[0]["m_002p"]
                  : "-",
                liuliang: res[0].data[0]["m_003q"]
                  ? res[0].data[0]["m_003q"]
                  : "-",
                reliang: res[0].data[0]["m_003qc"]
                  ? res[0].data[0]["m_003qc"]
                  : "-"
              };
            });
            break;

          default:
            break;
        }

        options.type = type

        // this.msgboxarr.push({ uid: uniqueID, diahtml: html, options: options });
        this.dependlistArr.cardpointbeijing.push({
          uid: uniqueID,
          options: options
        });
      });
    }
  }
};

export default zz;
